<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="报表日期" prop="bbrq">
        <el-input
          v-model="queryParams.bbrq"
          placeholder="请输入报表日期"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="记录人" prop="jlr">
        <el-input
          v-model="queryParams.jlr"
          placeholder="请输入记录人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报表编号" prop="bbbh">
        <el-input
          v-model="queryParams.bbbh"
          placeholder="请输入报表编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >生成当月报表</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['Productionline_system:sample:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['Productionline_system:sample:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['Productionline_system:sample:remove']"
          >删除</el-button
        >
      </el-col>
     <!--  <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['Productionline_system:sample:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="sampleList"
      @selection-change="handleSelectionChange"
      max-height="550"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="报表编号" align="center" prop="id" />
      <el-table-column label="胚" align="center" prop="p1" />
      <el-table-column label="盖" align="center" prop="g1" />
      <el-table-column label="标签" align="center" prop="bq1" />
      <el-table-column label="纸箱" align="center" prop="zx1" />
      <el-table-column label="胚" align="center" prop="p2" />
      <el-table-column label="盖" align="center" prop="g2" />
      <el-table-column label="标签" align="center" prop="bq2" />
      <el-table-column label="纸箱" align="center" prop="zx2" />
      <el-table-column label="胚" align="center" prop="p3" />
      <el-table-column label="盖" align="center" prop="g3" />
      <el-table-column label="标签" align="center" prop="bq3" />
      <el-table-column label="纸箱" align="center" prop="zx3" />
      <el-table-column label="报表日期" align="center" prop="bbrq" />
      <el-table-column label="产品规格" align="center" prop="cpgg" />
      <el-table-column label="记录人" align="center" prop="jlr" />
      <el-table-column label="报表编号" align="center" prop="bbbh" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="180px"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['Productionline_system:sample:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['Productionline_system:sample:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改制程检验原辅料消耗统计对话框 -->
    <el-dialog v-myFullscreen :title="title" :visible.sync="open" width="900px" append-to-body fullscreen>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="胚" prop="p1">
              <el-input
                v-model="form.p1"
                placeholder="请输入胚"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="盖" prop="g1">
              <el-input
                v-model="form.g1"
                placeholder="请输入盖"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="标签" prop="bq1">
              <el-input
                v-model="form.bq1"
                placeholder="请输入标签"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="纸箱" prop="zx1">
              <el-input
                v-model="form.zx1"
                placeholder="请输入纸箱"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="胚" prop="p2">
              <el-input
                v-model="form.p2"
                placeholder="请输入胚"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="盖" prop="g2">
              <el-input
                v-model="form.g2"
                placeholder="请输入盖"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="标签" prop="bq2">
              <el-input
                v-model="form.bq2"
                placeholder="请输入标签"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="纸箱" prop="zx2">
              <el-input
                v-model="form.zx2"
                placeholder="请输入纸箱"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="胚" prop="p3">
              <el-input
                v-model="form.p3"
                placeholder="请输入胚"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="盖" prop="g3">
              <el-input
                v-model="form.g3"
                placeholder="请输入盖"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="标签" prop="bq3">
              <el-input
                v-model="form.bq3"
                placeholder="请输入标签"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="纸箱" prop="zx3">
              <el-input
                v-model="form.zx3"
                placeholder="请输入纸箱"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="基地" prop="jd">
              <el-input
                v-model="form.jd"
                placeholder="请输入基地"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报表日期" prop="bbrq">
              <el-input
                v-model="form.bbrq"
                placeholder="请输入报表日期"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品规格" prop="cpgg">
              <el-input
                v-model="form.cpgg"
                placeholder="请输入产品规格"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="记录人" prop="jlr">
              <el-input
                v-model="form.jlr"
                placeholder="请输入记录人"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报表编号" prop="bbbh">
              <el-input
                v-model="form.bbbh"
                placeholder="请输入报表编号"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                placeholder="请输入备注"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!openDisabled">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSample,
  getSample,
  delSample,
  addSample,
  updateSample,
} from "@/api/quality/productionline/sample";

export default {
  name: "Sample",
  data() {
    return {
      //弹窗是否禁止编辑
      openDisabled: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 制程检验原辅料消耗统计表格数据
      sampleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        bbrq: null,
        jlr: null,
        bbbh: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询制程检验原辅料消耗统计列表 */
    getList() {
      this.loading = true;
      listSample(this.queryParams).then((response) => {
        this.sampleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        p1: null,
        g1: null,
        bq1: null,
        zx1: null,
        p2: null,
        g2: null,
        bq2: null,
        zx2: null,
        p3: null,
        g3: null,
        bq3: null,
        zx3: null,
        jd: null,
        bbrq: null,
        cpgg: null,
        jlr: null,
        bbbh: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.openDisabled = false;
      this.open = true;
      this.title = "添加制程检验原辅料消耗统计";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.openDisabled = false;
      const id = row.id || this.ids;
      getSample(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改制程检验原辅料消耗统计";
      });
    },
    /** 查看按钮 */
    handleView(row) {
      this.reset();
      this.openDisabled = true;
      const id = row.id || this.ids;
      getSample(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "查看制程检验原辅料消耗统计";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {

        if (valid) {
            /**打开遮罩层*/
            const rLoading = this.openLoading();
          if (this.form.id != null) {
            updateSample(this.form)
              .then((response) => {
                /**成功后执行关闭遮罩层*/
                rLoading.close();
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .catch((error) => {
                /**请求异常后执行关闭遮罩层*/
                rLoading.close();
              });
          } else {
            addSample(this.form)
              .then((response) => {
                /**成功后执行关闭遮罩层*/
                rLoading.close();
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .catch((error) => {
                /**请求异常后执行关闭遮罩层*/
                rLoading.close();
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除制程检验原辅料消耗统计编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delSample(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "Productionline_system/sample/export",
        {
          ...this.queryParams,
        },
        "制程检验原辅料消耗统计" + this.parseTime(new Date()) + ".xlsx"
      );
    },
  },
};
</script>
